package cn.tedu.service.customer.impl;

import cn.tedu.commons.exception.ServiceException;
import cn.tedu.commons.response.ServiceCode;
import cn.tedu.mapper.customer.CustomerAddrMapper;
import cn.tedu.mapper.customer.CustomerLoginMapper;
import cn.tedu.pojo.customer.dto.CustomerAddrDTO;
import cn.tedu.pojo.customer.entity.CustomerAddr;
import cn.tedu.pojo.customer.vo.CustomerAddrVO;
import cn.tedu.service.customer.ICustomerAddrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class CustomerAddrServiceImpl implements ICustomerAddrService {

    @Autowired
    private CustomerAddrMapper customerAddrMapper;

    @Autowired
    private CustomerLoginMapper customerLoginMapper;

    // 下面方法还没实现   无法传入id 给addr
   /* @Override
    public CustomerAddr insert(CustomerAddr customerAddr) {
    //获取用户id
        return null;
    }*/

    /*  // 下面方法还没实现   无法传入id 给addr
     *//**
     * 根据提供的用户名 查找地址
     * *//*
    @Override
    public CustomerAddrVO getAddrByUserName(String userName,Long userId) {
        //通过用户名获取customerLogin里的id
        CustomerLogin id= customerLoginMapper.getIdByUserName(userName);
        //通过该id 对应到用户地址
        CustomerAddrVO addrVO = customerAddrMapper.getAddrByUserId(id);
        System.out.println(addrVO);
        return addrVO;
    }*/



    /**
     * 需改地址*/
    @Override
    public CustomerAddrDTO updateAddr(CustomerAddrDTO customerAddrDTO) {
        //判断所提交的新地址，是否在对应userId下的原表中存在
        CustomerAddr isExist= customerAddrMapper.getAddrByOldAddr(customerAddrDTO.getUserAddr(),customerAddrDTO.getUserId());
        if (isExist==null){
            System.out.println("地址不存在");
            return customerAddrDTO;
        }
        CustomerAddr customerAddr=new CustomerAddr();
        customerAddr.setUserAddr(customerAddrDTO.getUserAddr());
        customerAddr.setGmtModified(LocalDateTime.now());
        customerAddrMapper.updateAddr(customerAddrDTO.getUserId(), customerAddrDTO.getUserAddr());
        return null;



    }

    /**
     * 根据id 查询地址
     * @Parm userId
     * @Author  WSH
     * @return*/
    @Override

    public CustomerAddrVO getAddrByUserId(Long userId) {
        CustomerAddr addrResult = customerAddrMapper.getAddrByUserId(userId);
        return null;
    }

    @Override
    public CustomerAddrVO getAddrByOldAddr(Long userId, String userAddr) {
        CustomerAddr addr = customerAddrMapper.getAddrByOldAddr(userAddr, userId);
        System.out.println(addr);
        return null;
    }

    @Override
    public CustomerAddrVO getAddrByDelete(Long userId, Integer isDelete) {
        CustomerAddrVO addr = customerAddrMapper.getAddrByDelete(userId, isDelete);
        System.out.println(addr);
        return null;
    }

    @Override
    public CustomerAddrVO updateAddDelete(Long userId, Integer isDelete) {
        CustomerAddr oldAddr = customerAddrMapper.getAddrByUserId(userId);
        if (oldAddr==null){
            throw new ServiceException(ServiceCode.NOT_FOUND,"该用户不存在地址");
            /*跳转增加地址*/
        }
        customerAddrMapper.updateAddrDelete(userId,isDelete);
        if (isDelete==1){
            log.debug("该用户地址为启用状态");
        }
        log.debug("该用户地址为启用状态");
        return null;
    }

    @Override
    public List<CustomerAddrVO> list() {
        return customerAddrMapper.list();
    }

}


